package cn.myframe.utils.aop;

import cn.myframe.entity.sys.CheckData;
import cn.myframe.service.sys.SysUtilsService;
import cn.myframe.utils.BaseDataUtils;
import cn.myframe.utils.annotation.CheckNoPass;
import cn.myframe.utils.core.R;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;


/**
 * 删除数据前，判断是否被关联
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017年3月8日 上午11:07:35
 */
@Aspect
@Component
public class CheckNoPassAspect {
	
	@Autowired
	private SysUtilsService sysUtilsService;
	
	@Pointcut("@annotation(cn.myframe.utils.annotation.CheckNoPass)")
	public void logPointCut() { 
		
	}
	
/*	@Before("logPointCut()")
	public void checkNoPass(JoinPoint joinPoint) {
		MethodSignature signature = (MethodSignature) joinPoint.getSignature();
		Method method = signature.getMethod();
		CheckNoPass associateData = method.getAnnotation(CheckNoPass.class);
		//请求的参数
		Object[] args = joinPoint.getArgs();
		List<Object> ids = JSON.parseArray(JSON.toJSONString(args[0]), Object.class);
		String status = null;
		if(args.length>=2){
			status = String.valueOf(args[1]);	
			if(!status.equals("3")&&!status.equals("9")&&!"7".equals(status)&&!"5".equals(status)){
				return;
			}
		}
		String value = associateData.value();
		if(StringUtils.isNotEmpty(value)){
			String[] datas = value.split(",");
			for(String deleteData : datas){
				CheckData data = AssociateData.associateData.get(deleteData);
				data.setIds(ids);
				List<Map<String,Object>> list = null;
				if(status.equals("7")){//撤销
					 list = sysUtilsService.checkRevoken(data);
				}else{
					if(data.getType().equals("1")){//不包含中间表
						 list = sysUtilsService.checkNoPassData(data);
					}else if(data.getType().equals("2")){//包含中间表
						list = sysUtilsService.checkMidNoPassData(data);
					}
				}
				
				if(list!=null&&list.size()>0){
					Map<String,Object> map = list.get(0);
                    String desc = "";
					if(data.getAssTable().equals("SYS_ROLE")){
						throw new RRException(BaseDataUtils.getEntityData(data.getAssDesc())+"["+map.get("assName")+"]"+BaseDataUtils.getSysterm("be")+BaseDataUtils.getEntityData(data.getDesc())+
								"["+map.get("name")+"]"+BaseDataUtils.getSysterm("checkAssociateNoUsed"));
					}else{
	                    if("7".equals(status)){
	                        desc = BaseDataUtils.getSysterm("checkAssociateRevoked");
	                    }else if("5".equals(status)){
	                        desc = BaseDataUtils.getSysterm("checkAssociateNotInject");
	                    }else{
	                        desc = BaseDataUtils.getSysterm("checkAssociateNoPass");
	                    }
						throw new RRException(BaseDataUtils.getEntityData(data.getAssDesc())+"["+map.get("assName")+"]"+BaseDataUtils.getSysterm("be")+BaseDataUtils.getEntityData(data.getDesc())+
								"["+map.get("name")+"]"+desc);
					}
				}
			}
		}
	}*/
	
	@Around("logPointCut()")
	public Object checkNoPass(ProceedingJoinPoint  joinPoint) throws Throwable {

		MethodSignature signature = (MethodSignature) joinPoint.getSignature();
		Method method = signature.getMethod();
		CheckNoPass associateData = method.getAnnotation(CheckNoPass.class);
		//请求的参数
		Object[] args = joinPoint.getArgs();
		List<Object> ids = JSON.parseArray(JSON.toJSONString(args[0]), Object.class);
		String status = null;
		if(args.length>=2){
			status = String.valueOf(args[1]);	
			if(!status.equals("3")&&!status.equals("9")&&!"7".equals(status)&&!"5".equals(status)&&!"12".equals(status)){
				Object o = joinPoint.proceed();
				return o;
			}
		}
		String value = associateData.value();
		if(StringUtils.isNotEmpty(value)){
			String[] datas = value.split(",");
			for(String deleteData : datas){
				CheckData data = AssociateData.associateData.get(deleteData);
				data.setIds(ids);
				List<Map<String,Object>> list = null;
				if(status.equals("7")){//撤销
					 list = sysUtilsService.checkRevoken(data);
				}else{
					if(data.getType().equals("1")){//不包含中间表
						 list = sysUtilsService.checkNoPassData(data);
					}else if(data.getType().equals("2")){//包含中间表
						list = sysUtilsService.checkMidNoPassData(data);
					}else if(data.getType().equals("3")){//黑名单
						list = sysUtilsService.checkMidNoPassData(data);
					}
				}
				
				if(list!=null&&list.size()>0){
					Map<String,Object> map = list.get(0);
                    String desc = "";
					if(data.getAssTable().equals("SYS_ROLE")||
							data.getDesc().equals("blackList")){
						return R.error(BaseDataUtils.getEntityData(data.getAssDesc())+"["+map.get("assName")+"]"+BaseDataUtils.getSysterm("be")+BaseDataUtils.getEntityData(data.getDesc())+
								"["+map.get("name")+"]"+BaseDataUtils.getSysterm("checkAssociateNoUsed"));
					}else{
	                    if("7".equals(status)){
	                        desc = BaseDataUtils.getSysterm("checkAssociateRevoked");
	                    }else if("5".equals(status)){
	                        desc = BaseDataUtils.getSysterm("checkAssociateNotInject");
	                    }else if("12".equals(status)){
							desc = BaseDataUtils.getSysterm("checkAssociateTerminate");
						}else{
	                        desc = BaseDataUtils.getSysterm("checkAssociateNoPass");
	                    }
	                    String name = map.get("name") == null ? "" : String.valueOf(map.get("name"));
						return R.error(data.getAssDesc()+"["+map.get("assName")+"]"+BaseDataUtils.getSysterm("be")+data.getDesc()+
								"["+name+"]"+desc);
					}
				}
			}
		}
		Object o = joinPoint.proceed();
		return o;
	
	}
	
}


